package com.atguigu.gmall.product.controller;


import com.atguigu.gmall.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FilenameUtils;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Api(tags = "上传管理接口")
@RestController
@RequestMapping("/admin/product")
public class FileUploadController {


    @Value("${fileServer.url}")
    public String fileUrl;

    //上传文件
    @ApiOperation("上传图片或文件")
    @PostMapping("/fileUpload")
    public Result fileUpload(MultipartFile file) throws Exception{
        String config = ClassUtils.getDefaultClassLoader().getResource("tracker.conf").getPath();
        ClientGlobal.init(config);
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer connection = trackerClient.getConnection();
        StorageClient1 storageClient1 = new StorageClient1(connection,null);
        String path = storageClient1.upload_file1(file.getBytes()
                , FilenameUtils.getExtension(file.getOriginalFilename()), null);
        return Result.ok(fileUrl + path);
    }

}
